<template>
  <transition name="fade">
    <div 
      v-if="visible" 
      class="scroll-to-top" 
      @click="scrollToTop"
      title="回到顶部"
    >
      <svg 
        xmlns="http://www.w3.org/2000/svg" 
        width="24" 
        height="24" 
        viewBox="0 0 24 24" 
        fill="none" 
        stroke="currentColor" 
        stroke-width="2" 
        stroke-linecap="round" 
        stroke-linejoin="round"
      >
        <polyline points="18 15 12 9 6 15"></polyline>
      </svg>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'ScrollToTop',
  data() {
    return {
      visible: false,
      scrollThreshold: 300 // 滚动超过300px后显示按钮
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeUnmount() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      this.visible = window.pageYOffset > this.scrollThreshold
    },
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      })
    }
  }
}
</script>

<style scoped>
.scroll-to-top {
  position: fixed;
  bottom: 40px;
  right: 40px;
  width: 50px;
  height: 50px;
  background-color: #00bcd4;
  color: white;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 188, 212, 0.3);
  transition: all 0.3s ease;
  z-index: 999;
}

.scroll-to-top:hover {
  background-color: #00acc1;
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(0, 188, 212, 0.4);
}

.scroll-to-top:active {
  transform: translateY(-1px);
}

.scroll-to-top svg {
  width: 24px;
  height: 24px;
}

/* 淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateY(10px);
}

@media (max-width: 768px) {
  .scroll-to-top {
    bottom: 20px;
    right: 20px;
    width: 45px;
    height: 45px;
  }
  
  .scroll-to-top svg {
    width: 20px;
    height: 20px;
  }
}
</style>

